package com.docparser.processor;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理分隔符（括号）元素。
 */
public class DelimiterProcessor extends BaseProcessor {
    private static final Map<String, String> DELIMITERS = new HashMap<String, String>() {{
        // 基本括号
        put("(", "\\left(");
        put(")", "\\right)");
        put("[", "\\left[");
        put("]", "\\right]");
        put("{", "\\left\\{");
        put("}", "\\right\\}");
        
        // 中文括号
        put("（", "\\left(");
        put("）", "\\right)");
        put("【", "\\left[");
        put("】", "\\right]");
        put("｛", "\\left\\{");
        put("｝", "\\right\\}");
        
        // 尖括号
        put("⟨", "\\left\\langle");
        put("⟩", "\\right\\rangle");
        put("〈", "\\left\\langle");
        put("〉", "\\right\\rangle");
        put("<", "\\left<");
        put(">", "\\right>");
        
        // 竖线
        put("|", "\\left|");
        put("‖", "\\left\\|");
        put("∥", "\\left\\|");
        put("｜", "\\left|");
        
        // 其他分隔符
        put("⌈", "\\left\\lceil");
        put("⌉", "\\right\\rceil");
        put("⌊", "\\left\\lfloor");
        put("⌋", "\\right\\rfloor");
    }};

    /**
     * 处理分隔符元素。
     */
    public static void process(Element delimElement, StringBuilder formula, ProcessorCallback callback) {
        if (delimElement == null) return;

        String left = null;
        String right = null;
        StringBuilder content = new StringBuilder();

        System.out.println("\n处理分隔符元素...");
        
        // 获取分隔符信息
        NodeList dPrList = delimElement.getElementsByTagNameNS(
            MATH_NAMESPACE, "dPr");
        if (dPrList.getLength() > 0) {
            Element dPrElement = (Element) dPrList.item(0);
            System.out.println("找到dPr元素");

            // 获取begChr和endChr元素
            NodeList begChrList = dPrElement.getElementsByTagNameNS(
                MATH_NAMESPACE, "begChr");
            NodeList endChrList = dPrElement.getElementsByTagNameNS(
                MATH_NAMESPACE, "endChr");

            if (begChrList.getLength() > 0) {
                left = begChrList.item(0).getTextContent();
                System.out.println("找到左分隔符: " + left);
            }
            if (endChrList.getLength() > 0) {
                right = endChrList.item(0).getTextContent();
                System.out.println("找到右分隔符: " + right);
            }
        }

        // 获取分隔符内容
        NodeList eList = delimElement.getElementsByTagNameNS(
            MATH_NAMESPACE, "e");
        if (eList.getLength() > 0) {
            callback.processElement((Element) eList.item(0), content);
            System.out.println("分隔符内容: " + content.toString());
        }
        
        processDelimiterPair(left, right, content.toString(), formula);
    }

    /**
     * 处理分隔符对。
     */
    private static void processDelimiterPair(String left, String right, String content, StringBuilder formula) {
        // 当Word文档中没有显式指定分隔符时，使用默认的圆括号
        String effectiveLeft = "\\left(";
        String effectiveRight = "\\right)";

        if (left != null && right != null) {
            effectiveLeft = getLatexDelimiter(left);
            effectiveRight = getLatexDelimiter(right);
        }

        System.out.println("左分隔符: " + effectiveLeft);
        System.out.println("右分隔符: " + effectiveRight);
        formula.append(effectiveLeft)
              .append(content)
              .append(effectiveRight);
    }

    /**
     * 获取LaTeX分隔符。
     */
    private static String getLatexDelimiter(String delimiter) {
        if (delimiter == null) return null;
        return DELIMITERS.getOrDefault(delimiter, delimiter);
    }
}
